package main

import (
	"fmt"
	"bytes"
)

type Time int64

func (a Time) GetBytes() []byte {
	buf := make([]byte, 8)
	i := 0
	buf[i] = byte(a >> 56)
	i++
	buf[i] = byte(a >> 48)
	i++
	buf[i] = byte(a >> 40)
	i++
	buf[i] = byte(a >> 32)
	i++
	buf[i] = byte(a >> 24)
	i++
	buf[i] = byte(a >> 16)
	i++
	buf[i] = byte(a >> 8)
	i++
	buf[i] = byte(a)

	return buf
}

func unixTimeTo8Bytes(time int64) []byte {
	buf := make([]byte, 8)
	i := 0
	buf[i] = byte(time)
	i++
	buf[i] = byte(time >> 8)
	i++
	buf[i] = byte(time >> 16)
	i++
	buf[i] = byte(time >> 24)
	i++
	buf[i] = byte(time >> 32)
	i++
	buf[i] = byte(time >> 40)
	i++
	buf[i] = byte(time >> 48)
	i++
	buf[i] = byte(time >> 56)
	return buf
}

func StringToBytesWithLen1(a string) []byte {
	contentBytes := []byte(a)

	result := make([]byte, 1+len(contentBytes))
	var i = 0
	result[i] = byte(len(contentBytes))
	i++
	for _, s := range contentBytes {
		result[i] = s
		i++
	}
	for _, s := range result {
		fmt.Printf(" %02X", s)
	}
	fmt.Println()
	return result
}

func StringToBytesWithLen2(a string) []byte {
	contentBytes := []byte(a)

	result := make([]byte, 2+len(contentBytes))
	var i = 0
	result[i] = byte(len(contentBytes))
	i++
	result[i] = byte(len(contentBytes) >> 8)
	i++
	for _, s := range contentBytes {
		result[i] = s
		i++
	}
	for _, s := range result {
		fmt.Printf("%02X", s)
	}
	fmt.Println()
	return result
}

func toHex(data []byte) string {
	var buf = bytes.NewBufferString("")
	for _, s := range data {
		buf.WriteString(fmt.Sprintf("%02X", s))
	}

	return buf.String()
}
